function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1}`
}

export function getQueryString(url,name) {
  console.log("url = "+url)
  console.log("name = " + name)
  var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
  var r = url.substr(1).match(reg)
  if (r != null) {
    console.log("r = " + r)
    console.log("r[2] = " + r[2])
    return r[2]
  }
  return null;
}

export function timeQuantum(time) {
  time = parseInt(time * 1000);//获取传入时间 并将其转化为date型
  var nowTime = Date.parse(new Date());//获取当前时间
  var timeDifference = nowTime - time;//时间差的毫秒数
  if (timeDifference < 0){
    return Math.abs(timeDifference)
  } else{
    return timeDifference
  }
}

export default {
  formatNumber,
  formatTime,
  timeQuantum,
  getQueryString
}
